﻿/*
 *  Copyright (c) 2012 Brandon Ellenberger
 *
 *  This file is part of OpSatPlan.
 *
 *  OpSatPlan is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpSatPlan is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpSatPlan.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using OpSatPlan.LexerParser;
using OpSatPlan.PlanningGraph;
using OpSatPlan.SatSolvers;

namespace OpSatPlan
{
    class OpSatPlan
    {
        static void Main(string[] args)
        {
            try
            {
                Domain domain;
                Problem problem;
                Input input;
                SatSolver solver;
                Planner planner;
                DomainProcessor domProc;
                ProblemProcessor probProc;
                IdentifierProcessor idProc;

                // start the timer
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Reset();
                stopwatch.Start();

                // parser commaind line
                input = new Input(args);
                Output.Verbose = input.Verbose;

                // get the domain
                domProc = new DomainProcessor(input.DomainFileName);
                domain = domProc.BuildDomain();

                // get the problem
                probProc = new ProblemProcessor(input.ProblemFileName, domain);
                problem = probProc.BuildProblem();

                // add the "identifiers" to the domain
                idProc = new IdentifierProcessor(input.IdentifierFileName, domain);
                idProc.BuildIdentifiers();

                // get the sat solver to use
                solver = SolverFactory.Instance.GetSolver(input.SolverName);

                planner = new Planner(domain, problem, solver);

                planner.Solve(input, stopwatch);

                stopwatch.Stop();


                Output.Write(string.Format("Total time to find Step Optimal Plan: {0}", stopwatch.ElapsedMilliseconds / 1000.0));
            }
            catch (UsageException ue)
            {
                Console.WriteLine(ue.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        public static bool IsRunningOnMono()
        {
            return Type.GetType("Mono.Runtime") != null;
        }

    }
}
